#include "graphicsview.h"

GraphicsView::GraphicsView(QWidget *parent) : QGraphicsView(parent)
{
    m_drawingMode = true;
}

void GraphicsView::mousePressEvent(QMouseEvent *event) {
    if (m_drawingMode) {
        m_prePos = mapToScene(event->pos()); // 获得当前鼠标位置，mapToScene 函数用于将视图坐标转换为场景坐标，以便在场景中进行绘制。
        emit drawingStarted(m_prePos);
    }
    QGraphicsView::mousePressEvent(event);
}

void GraphicsView::mouseMoveEvent(QMouseEvent *event) {
    if (m_drawingMode && (event->buttons() & Qt::LeftButton)) {
        QPointF newPos = mapToScene(event->pos());
        emit drawingMoved(newPos);
    }
    QGraphicsView::mouseMoveEvent(event);
}

void GraphicsView::mouseReleaseEvent(QMouseEvent *event) {
    if (event->button() == Qt::LeftButton) {
           QPointF endPos = mapToScene(event->pos());
           emit drawingEnd(endPos);
       }
    QGraphicsView::mouseReleaseEvent(event);
}
